package com.log.service.Impl;

import com.log.common.util.PageHelper;
import com.log.common.vo.ResStatus;
import com.log.common.vo.ResultVo;
import com.log.dao.LogCarMapper;
import com.log.pojo.LogCar;
import com.log.pojo.LogGoodsOutIn;
import com.log.service.LogCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
*
* @author 王泽旺
* @date 2023/10/20
*/
@Service
public class LogCarServiceImpl implements LogCarService {
    @Autowired
    private LogCarMapper logCarMapper;

    /**
     * 添加车辆信息
     * @param logCar
     * @return
     */
    @Override
    public ResultVo insertCarInfo(LogCar logCar) {
        Example example = new Example(LogCar.class);
        Example.Criteria criteria = example.createCriteria();
        //通过车牌号判断是否存在此车辆
        criteria.andEqualTo("logCarPlate", logCar.getLogCarPlate());
        List<LogCar> logCars = logCarMapper.selectByExample(example);
        if(logCars.size()>0)
        {
            //该车辆已入库
            return new ResultVo(ResStatus.NO,"该车辆已入库",null);
        }else{
            //将该车辆添加进入数据库
            logCar.setLogPurchaseTime(new Date());
            logCarMapper.insert(logCar);
            return new ResultVo(ResStatus.OK,"添加车辆成功",null);
        }
    }

    /**
     * 通过id查询车辆
     * @param id
     * @return
     */
    @Override
    public ResultVo getCarInfoById(int id) {
        LogCar logCar = logCarMapper.selectByPrimaryKey(id);
        return logCar==null?new ResultVo(ResStatus.NO,"查询记录不存在",logCar):new ResultVo(ResStatus.OK,"success",logCar);
    }

    /**
     * 分页查询全部车辆
     * @param pageNum
     * @param limit
     * @return
     */
    @Override
    public ResultVo getAllCarInfoByPage(int pageNum, int limit) {
        //1.起始索引
        int start = (pageNum-1)*limit;
        //2.查询结果
        List<LogCar> logCars = logCarMapper.selectLogCarByPage(start, limit);
        //3.查询总数
        Example example = new Example(LogCar.class);
        int count = logCarMapper.selectCountByExample(example);
        //3.计算总页数
        int pageCount = count%limit==0 ? count/limit:count/limit+1;
        return new ResultVo(ResStatus.OK,"success",new PageHelper<LogCar>(count,pageCount,logCars));
    }

    @Override
    public ResultVo getAllCarInfoByPageAndStatus(int pageNum, int limit, int status) {
        //1.起始索引
        int start = (pageNum-1)*limit;
        //2.查询结果
        List<LogCar> logCars = logCarMapper.selectLogCarByPageAndStatus(start, limit,status);
        //3.查询总数
        Example example = new Example(LogCar.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("logCarStatus",status);
        int count = logCarMapper.selectCountByExample(example);
        //3.计算总页数
        int pageCount = count%limit==0 ? count/limit:count/limit+1;
        return new ResultVo(ResStatus.OK,"success",new PageHelper<LogCar>(count,pageCount,logCars));
    }


    /**
     * 更新车辆信息
     * @param logCar
     * @return
     */
    @Override
    public ResultVo updateCarInfo(LogCar logCar) {
        return new ResultVo(ResStatus.OK,"更新成功",logCarMapper.updateByPrimaryKey(logCar));
    }


}
